

import 'package:single/event/user_id_event.dart';
import 'package:single/event/check_status_event.dart';
import 'package:single/util/convert_util.dart';

import '../event/door_status_event.dart';
import '../event/inventory_event.dart';
import '../event/light_status_event.dart';
import '../main.dart';
import 'send_helper.dart';

class AnalysisHelper{

  String result = "";
  bool isAnalyze = false;

  Set<String> epcData = {};
  Set<String> blockData = {};
  var inventoryTime = settingEntity.scanTime??1;

  var dataIndex = 0;
  var readBlockAnt = 1;
  var hasChange = false;


  dataReceived(hexString){

    //沾包
    if(!hexString.startsWith('DD11') && result.isNotEmpty ){
      result += hexString;
      analyze();
      return;
    }
    //询查标签
    if(hexString.substring(10,12) == "01"){
      result += hexString;
      analyze();
      return;
    }

    //读块
    if(hexString.substring(10,12)=="23"){
      analyzeBlock(hexString);
      return;
    }

    //读读者卡
    if(hexString.substring(0,2)=="AA"){
      analyzeId(hexString);
      return;
    }
    //开灯 关灯
    if(hexString.substring(10,16)=="C15900"){
      eventFactory.fire(LightStatusEvent());
      return;
    }
    //开门
    if(hexString.substring(10,16)=="C25900"){
      eventFactory.fire(DoorStatusEvent(true));
      return;
    }
    //查询状态
    if(hexString.substring(10,14)=="C359" && hexString.substring(22,24)=="00"){
      checkStatus(hexString);
      return;
    }
    //关门
    if(hexString.substring(10,14)=="C359" && hexString.substring(22,24)=="01"){
      eventFactory.fire(DoorStatusEvent(false));
      return;
    }

  }

  analyzeId(String ids){
    String id = ids;
    if(ids.endsWith('AA01EA')){
      id = ids.replaceAll('AA01EA', '');
    }
    if(id.length>10){
      String uid = id.substring(id.length-8,id.length);
      String exUid = uid.substring(6,8)+uid.substring(4,6)+uid.substring(2,4)+uid.substring(0,2);
      String uidNo = hexToInt(exUid).toString();
      while(uidNo.length<10){
        uidNo = '0$uidNo';
      }
      eventFactory.fire(UserIdEvent(uidNo));
    }
  }

  analyze(){
    if(isAnalyze){
      return;
    }
    isAnalyze = true;
    //盘点结束指令
    if(result.startsWith("DD11EF0CFF01")){
      inventoryTime--;
      if(inventoryTime >0){
        result = "";
        isAnalyze = false;
        hfSerialPortHelper.sendData(inventory());
      }else{
        if(settingEntity.isUltra! || epcData.isEmpty){
          var data = epcData.toList();
          sendAnalyzeData(data);
        }else{
          startReadBlock();
        }
      }
      return;
    }
    if(result.length>=38){
      epcData.add(result.substring(18,34));
      result = result.substring(38);
      isAnalyze = false;
      if(result.isNotEmpty){
        analyze();
      }
    }else{
      isAnalyze = false;
    }
  }

  sendAnalyzeData(data){
    eventFactory.fire(InventoryEvent(data));
    result = "";
    epcData.clear();
    inventoryTime = settingEntity.scanTime??1;
    hasChange = false;
    blockData.clear();
    readBlockAnt = 1;
    dataIndex = 0;
    isAnalyze = false;
  }

  startReadBlock(){
    hfSerialPortHelper.sendData(readBlock(readBlockAnt,epcData.toList()[dataIndex]));
  }

  analyzeBlock(hexString) {
    //没盘到
    if (hexString.length > 14 && hexString.substring(12, 14) == "95") {
      //更换天线
      if(!hasChange){
        hasChange = true;
        if(readBlockAnt == 1){
          readBlockAnt = 2;
        }else{
          readBlockAnt = 1;
        }
        startReadBlock();
        return;
      }else{
        //下个标签
        dataIndex++;
        if(dataIndex<epcData.length){
          hasChange = false;
          startReadBlock();
        }else{
          var data = blockData.toList();
          sendAnalyzeData(data);
        }
      }
      return;
    }
    //盘到了
    String blockCount = hexString.substring(12, 14);
    int count = hexToInt(blockCount);
    if (hexString.length >= 16+count*2+count*8+4) {
      var block = '';
      for(int i = 0; i<count;i++){
        int start = 16+(i+1)*2+i*8;
        block = block + hexString.substring(start,start+8);
      }
      blockData.add(block);
      dataIndex++;
      if(dataIndex<epcData.length){
        hasChange = false;
        startReadBlock();
      }else{
        var data = blockData.toList();
        sendAnalyzeData(data);
      }

    }
  }

  checkStatus(hexString){
    bool light1 = false;
    bool light2 = false;
    bool door = false;
    if(hexString.substring(14,16)=="00"){
      light1 = false;
    }else{
      light1 = true;
    }
    if(hexString.substring(16,18)=="00"){
      light2 = false;
    }else{
      light2 = true;
    }
    if(hexString.substring(18,20)=="01"){
      door = false;
    }else{
      door = true;
    }
    eventFactory.fire(CheckStatusEvent(light1, light2,door));

  }


}